﻿using System;
using System.Collections.Generic;
 
using System.Text;
using Cabbage.MySQLServerDAL;
using System.Data;

namespace mole.user
{
    class userx
    {


        public static bool test = false;
        public static bool isadminlogin = false;
        public static bool a1 = false;
        public static bool a2 = false;
        public static bool a3 = false;

        public static bool b1 = false;
        public static bool b2 = false;
        public static bool b3 = false;
        internal bool login(string username, string pwd)
        {
            if (username.Equals("admin"))

                return adminlogin(username, pwd);
            else
            {
                DataSet ds = DbHelperMySQL.Query("select * from customers_copy where username='" + username + "'");
                DataTable dt = ds.Tables[0];//     Tables

                if (dt.Rows.Count == 0) return false;
                string p = dt.Rows[0]["password"].ToString();
                if (pwd.Equals(p))
                {
                    
                        isadminlogin = false;
                        a1 = bool.Parse(dt.Rows[0]["a1"].ToString());
                        a2 = bool.Parse(dt.Rows[0]["a2"].ToString());
                        b1 = bool.Parse(dt.Rows[0]["b1"].ToString());
                        b2 = bool.Parse(dt.Rows[0]["b2"].ToString());
                        return true;

                }
                else 
                    
                    return false;


            }




        }

        internal bool adminlogin(string username, string pwd)
        {
            DataSet ds = DbHelperMySQL.Query("select * from customers where username='" + username + "'");
            DataTable dt = ds.Tables[0];//     Tables

            if (dt.Rows.Count == 0) return false;
            string p = dt.Rows[0]["pwd4net"].ToString();
            if (pwd.Equals(p))
            {
                if (username.Equals("admin"))
                    isadminlogin = true;
                else
                    isadminlogin = false;
                return true;

            }
            else return false;
        }

        internal bool notExistUser(string name)
        {
            DataSet ds = DbHelperMySQL.Query("select * from customers_copy where username='" + name + "'");
            DataTable dt = ds.Tables[0];//     Tables

            if (dt.Rows.Count == 0) return true;
            else
                return false;
        }


        public DataTable  loadUsers( )
        {
            DataSet ds = DbHelperMySQL.Query("select * from customers_copy  order by id desc");
            DataTable dt = ds.Tables[0];//     Tables

            return dt;
        }
        //public bool a1; public bool a2;
        //public bool a3;
        //public bool b1; public bool b2;
        //public bool b3;

        internal void adduser(string name, string pwd)
        {
             

            string sql = "        insert into customers_copy(username,password,a1,a2,a3,b1,b2,b3) values('@name','@pwd','@a1','@a2','@a3','@b1','@b2','@b3')";
            sql = sql.Replace("@name", name); sql = sql.Replace("@pwd", pwd);
            sql = sql.Replace("@a1",  a1.ToString()); sql = sql.Replace("@a2",  a2.ToString()); sql = sql.Replace("@a3",  a3.ToString());

            sql = sql.Replace("@b1",  b1.ToString()); sql = sql.Replace("@b2",b2.ToString()); sql = sql.Replace("@b3", b3.ToString());

            DbHelperMySQL.ExecuteSql(sql);
        }
    }
}
